# AudioPlayScreen - This screen displays information of currently playing audio
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

# Widgets
from frontend.gui.widgets.tabs_menu import TabsMenu
from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.image_menu import ImageMenu

from frontend.gui.widgets.text_menu_item import TextMenuItem
from frontend.gui.widgets.image_menu_item import ImageMenuItem
from frontend.gui.widgets.justified_text_menu_item import JustifiedTextMenuItem

from frontend.gui.widgets.list_indicator import ListIndicator

# Music library
from frontend.medialibrary.music import MusicLibrary
from frontend.medialibrary.music import Track
from frontend.medialibrary.music import Album

class AudioPlayScreen(Screen):
    """
    AudioPlayScreen
    
    Screen that displays information of currently playing audio track.
    """
    
    def __init__(self, ui, frontend, track):
        """
        Initialize screen
        
        @param ui: UserInterface object
        @param frontend: FrontendClient object
        @param track: Track object (We display information of this track)
        """
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.library = self.frontend.get_music_library()
        self.group = clutter.Group()
        self.track = track
        self.album = track.get_album()
        
        # Create and initialize screen items
        self.current_tab = "currently_playing"
        self._create_tabs()
        self._create_currently_playing_tab()
        self._create_playlist_tab()
        
        self.group.show()
        ui.getStage().add(self.group)
        
        self.ui.get_media_player().set_media(self.track)
        self.ui.get_media_player().play()

    def _create_tabs(self):
        """
        Create tab bar of the screen. Tabs: Currently playing, playlist
        """
        self.tabs = TabsMenu(self.theme, 
                        self.frontend.get_configuration().show_effects())
        self.tabs.set_item_size(400, 60)
        self.tabs.set_visible_column_count(4)
        
        tab1 = JustifiedTextMenuItem(400, 60, self.theme, "Currently playing", JustifiedTextMenuItem.CENTER)
        tab1.set_userdata("currently_playing")
        self.tabs.add_actor(tab1)
        
        tab2 = JustifiedTextMenuItem(400, 60, self.theme, "Playlist", JustifiedTextMenuItem.CENTER)
        tab2.set_userdata("playlist")
        self.tabs.add_actor(tab2)
        
        self.tabs.set_position(283, 40)
        self.tabs.set_active(True)
        self.tabs.show()
        self.group.add(self.tabs)
    
    def _create_currently_playing_tab(self):
        """
        Create a tab that shows information on currenlty playing audio track.
        """
        self.currently_playing_tab = clutter.Group()
        
        if(self.album.has_album_art()):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.album.get_album_art_url())
        else:
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_album_art"))
        self.art = clutter.Texture()
        self.art.set_pixbuf(pixbuf)
        self.art.set_position(self.ui.get_abs_x_pos(0.1),self.ui.get_abs_y_pos(0.2))
        self.art.set_size(430,430)
        self.art.show()
        self.currently_playing_tab.add(self.art)
        
        self.track_label = clutter.Label()
        self.track_label.set_font_name(self.theme.getFont() + ' 42px')
        self.track_label.set_color(self.theme.getColor("text"))
        self.track_label.set_text(str(self.track.get_tracknumber()) + ". " + self.track.get_title())
        self.track_label.set_size(700, 50)
        self.track_label.set_position(self.ui.get_abs_x_pos(0.5), self.ui.get_abs_y_pos(0.35))
        self.track_label.show()
        self.currently_playing_tab.add(self.track_label)
        
        self.album_label = clutter.Label()
        self.album_label.set_font_name(self.theme.getFont() + ' 38px')
        self.album_label.set_color(self.theme.getColor("subtitle"))
        self.album_label.set_text("From " + self.album.get_title() + ", " + str(self.album.get_year()))
        self.album_label.set_size(700, 50)
        self.album_label.set_position(self.ui.get_abs_x_pos(0.5), self.ui.get_abs_y_pos(0.45))
        self.album_label.show()
        self.currently_playing_tab.add(self.album_label)
        
        self.artist_label = clutter.Label()
        self.artist_label.set_font_name(self.theme.getFont() + ' 38px')
        self.artist_label.set_color(self.theme.getColor("subtitle"))
        self.artist_label.set_text("By " + self.album.get_artist())
        self.artist_label.set_size(700, 50)
        self.artist_label.set_position(self.ui.get_abs_x_pos(0.5), self.ui.get_abs_y_pos(0.55))
        self.artist_label.show()
        self.currently_playing_tab.add(self.artist_label)
        
        self.currently_playing_tab.show()
        self.group.add(self.currently_playing_tab)

    def _create_playlist_tab(self):
        """
        Create a tab that displays tracks on current playlist.
        """
        self.playlist_tab = clutter.Group()
        self.group.add(self.playlist_tab)

    def _change_tab(self):
        """
        Change tab. This is called when user changes the screen tab.
        """
        self.currently_playing_tab.hide()
        self.playlist_tab.hide()
            
        self.current_tab = self.tabs.get_current_menuitem().get_userdata()
        
        if self.current_tab == "currently_playing":
            self.currently_playing_tab.show()
        else:
            self.playlist_tab.show()
    
    def update(self, track):
        """
        This is called when track changes.
        @param track: Currently playing track
        """
        pass
    
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "AudioPlayScreen"
    
    def handle_key_press_event(self, event):   
        """ Handle key pressed event."""
        #UP KEY
        if event.keyval == clutter.keysyms.Up:
            pass
        
        #DOWN KEY  
        elif event.keyval == clutter.keysyms.Down:
            pass
        
        #LEFT KEY
        elif event.keyval == clutter.keysyms.Left:
            if self.tabs.is_active():
                self.tabs.move(self.tabs.LEFT)
                self._change_tab()
        
        # RIGHT KEY
        elif event.keyval == clutter.keysyms.Right:
            if self.tabs.is_active():
                self.tabs.move(self.tabs.RIGHT)
                self._change_tab()
        
        # RETURN KEY
        elif event.keyval == clutter.keysyms.Return:
            pass


